package weaver.jobs.ztj;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-08-17
 * @Description: 定时任务删除发票信息到ERP
 * @ModifyLog:
 **/

public class WeaRemoveInvoiceCronJob extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            String currentDate = DateUtil.getCurrentDate();
            baseBean.writeLog(">>>>WeaRemoveInvoiceCronJob>>>currentDate=" + currentDate);
            RecordSet rs = new RecordSet();
            rs.executeQuery("select log_id from Fnalog where log_type_id=1 and log_source = 'FnaInvoiceLedger'" +
                    " and log_date='"+currentDate+"' and ifnull(log_add_type_1,0)=0 ");
            while (rs.next()) {
                JSONObject paramJson = new JSONObject();
                JSONArray detailList = new JSONArray();
                JSONObject detailJson = new JSONObject();
                String logId = Util.null2String(rs.getString("log_id"));
                baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>logId=" + logId);
                if (!"".equals(logId)) {
                    detailJson.put("wbid", logId);
                    detailJson.put("operate", "2");
                    detailList.add(detailJson);
                    JSONObject dataJson = new JSONObject();
                    dataJson.put("Details", detailList);
                    JSONObject DataJson = new JSONObject();
                    DataJson.put("Data", dataJson);
                    paramJson.put("data", DataJson);
                    baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>paramJson=" + paramJson.toJSONString());
                    String returnStr = sendOKHttp3Request(paramJson.toJSONString());
                    JSONObject returnJson = JSONObject.parseObject(returnStr);
                    baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>returnJson=" + returnJson.toJSONString());
                    String success = Util.null2String(returnJson.getString("success"));
                    if ("true".equals(success)) {
                        boolean flag = rs.execute("update Fnalog set log_add_type_1=1 where log_id=" + logId);
                        baseBean.writeLog(">>>>WeapushInvoiceCronJob>>>flag=" + flag);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String sendOKHttp3Request(String paramJson) {
        String result = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody body = RequestBody.create(mediaType, "");
            String url = "http://58.241.13.10:11896/Api/PutData/ExecProcedureWithReturnJsonError";
            String dataflow = "dataflow=泛微oa→RZERPv4_铁建工科";
            String actionCode = "actionCode=put_gyfpb";

            String data = "data=" + paramJson;
            Request request = new Request.Builder()
                    .url(url + "?" + dataflow + "&" + actionCode + "&" + data)
                    .method("POST", body)
                    .addHeader("Accept", "*/*")
                    .build();
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


}

